package com.ysd.lis.service.lab.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabBarCodeType;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.mapper.lab.LabBarCodeTypeMapper;
import com.ysd.lis.mapper.sys.SysBasDictDetailMapper;
import com.ysd.lis.request.AddBarCodeTypeParam;
import com.ysd.lis.request.BarCodeTypePageParam;
import com.ysd.lis.service.SysOrganizationService;
import com.ysd.lis.service.lab.LabBarCodeTypeService;
import com.ysd.lis.service.lab.LabRptGroupService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 分管类别 服务实现类
 * </p>
 *
 * @author Cfz
 * @since 2023-12-12
 */
@Service
public class LabBarCodeTypeServiceImpl extends ServiceImpl<LabBarCodeTypeMapper, LabBarCodeType> implements LabBarCodeTypeService {

    @Autowired
    private LabBarCodeTypeMapper labBarCodeTypeMapper;
    @Autowired
    private LabRptGroupService rptGroupService;
    @Autowired
    private SysOrganizationService sysOrganizationService;
    @Autowired
    private SysBasDictDetailMapper basDictDetailMapper;


    @Override
    public Result findLabBarCodeTypeList(BarCodeTypePageParam barCodeTypePageParam) {
        MPJLambdaWrapper<LabBarCodeType> lambdaQueryWrapper = new MPJLambdaWrapper();
        lambdaQueryWrapper.selectAll(LabBarCodeType.class);
        lambdaQueryWrapper.eq(LabBarCodeType::getDelFlag, 0);
        lambdaQueryWrapper.selectAs("smt", LabSampleType::getSampleTypeName, LabBarCodeType::getSampleTypeName);
        lambdaQueryWrapper.selectAs("rptg", LabRptGroup::getRptGroupName, LabBarCodeType::getRptGroupName);

        lambdaQueryWrapper.leftJoin(LabSampleType.class, "smt", p -> p.eq(LabSampleType::getSampleTypeCode, LabBarCodeType::getSampleTypeCode).eq(LabSampleType::getDelFlag, 0).eq(LabSampleType::getOrgId, LabBarCodeType::getOrgId).isNotNull(LabSampleType::getSampleTypeCode).ne(LabSampleType::getSampleTypeCode, ""));
        lambdaQueryWrapper.leftJoin(LabRptGroup.class, "rptg", p -> p.eq(LabRptGroup::getRptGroup, LabBarCodeType::getRptGroup).eq(LabRptGroup::getDelFlag, 0).eq(LabRptGroup::getOrgId, LabBarCodeType::getOrgId).isNotNull(LabRptGroup::getRptGroup).ne(LabRptGroup::getRptGroup, ""));


        if (StringUtils.isNotBlank(barCodeTypePageParam.getOrgId())) {
            lambdaQueryWrapper.eq(LabBarCodeType::getOrgId, barCodeTypePageParam.getOrgId());
        }

        if (StrUtil.isNotEmpty(barCodeTypePageParam.getKeyWords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabBarCodeType::getBarcodeNo, barCodeTypePageParam.getKeyWords()).or().like(LabBarCodeType::getBarcodeNa, barCodeTypePageParam.getKeyWords()).or().like(LabBarCodeType::getShortName, barCodeTypePageParam.getKeyWords()).or().like(LabBarCodeType::getMa, barCodeTypePageParam.getKeyWords()));
        }

        List<LabBarCodeType> labBarCodeTypes = labBarCodeTypeMapper.selectList(lambdaQueryWrapper);

        return Result.succ(1, "查询成功！", labBarCodeTypes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addBarCodeType(LabBarCodeType labBarCodeType) {
        //LabBarCodeType labBarCodeType = new LabBarCodeType();
        //BeanUtil.copyProperties(addBarCodeTypeParam, labBarCodeType);
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            sysUser.setOrgId(sysUser.getOrgId());
        }
        boolean save = this.save(labBarCodeType);

        if (save) {
            return Result.succ(1, "添加成功！", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result editBarCodeType(LabBarCodeType labBarCodeType) {
        //LabBarCodeType labBarCodeType = new LabBarCodeType();
        //BeanUtil.copyProperties(addBarCodeTypeParam, labBarCodeType);
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            sysUser.setOrgId(sysUser.getOrgId());
        }
        boolean b = this.updateById(labBarCodeType);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result deleteBarCodeType(AddBarCodeTypeParam addBarCodeTypeParam) {
        LabBarCodeType labBarCodeType = new LabBarCodeType();
        BeanUtil.copyProperties(addBarCodeTypeParam, labBarCodeType);
        labBarCodeType.setDelFlag(1);
        boolean b = this.updateById(labBarCodeType);
        if (b) {
            return Result.succ(1, "删除成功！", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
